View Javadoc

1   /*
2    * Copyright 2004-2005, University Health Network.  All rights reserved. Distributed under the BSD 
3    * license (see http://opensource.org/licenses/bsd-license.php).
4    *  
5    * MethodDataSource.java
6    *
7    * Created on 21-Dec-2004 at 3:19:43 PM
8    */
9   package ca.uhn.cache.impl;
10  
11  import java.lang.reflect.InvocationTargetException;
12  import java.lang.reflect.Method;
13  
14  import ca.uhn.cache.IDataSource;
15  import ca.uhn.cache.IMethodDataSourceHelper;
16  import ca.uhn.cache.IQuery;
17  import ca.uhn.cache.IQueryResult;
18  import ca.uhn.cache.exception.DataSourceException;
19  import ca.uhn.cache.exception.MethodDataSourceIllegalAccessException;
20  import ca.uhn.cache.exception.MethodDataSourceInvocationTargetException;
21  
22  
23  /***
24   * <code>IDataSource</code> implementation backed by a method invocation. 
25   * 
26   * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara</a>
27   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:52:37 $ by $Author: bryan_tripp $
28   */
29  public class MethodDataSource implements IDataSource {
30      
31      private Method myMethod;
32      private Object myInstance;
33      private IMethodDataSourceHelper myMethodDataSourceHelper;
34  
35      /***
36       * 
37       */
38      public MethodDataSource() {
39          super();
40      }
41  
42      /***
43       * {@inheritDoc}
44       */
45      public IQueryResult execute( IQuery[] theQueries ) throws DataSourceException {
46          IQueryResult retVal = new QueryResult();
47          
48          for (int i = 0; i < theQueries.length; i++) {
49              IQuery query = theQueries[i];
50              
51              Object[] arguments = getMethodDataSourceHelper().buildArguments( query );
52              
53              Object object;
54              try {
55                  object = myMethod.invoke( myInstance, arguments );
56              }
57              catch (IllegalArgumentException e) {
58                  throw e;
59              }
60              catch (IllegalAccessException e) {
61                  throw new MethodDataSourceIllegalAccessException( e );
62              }
63              catch (InvocationTargetException e) {
64                  throw new MethodDataSourceInvocationTargetException( e );
65              }
66  
67              IQueryResult qr = getMethodDataSourceHelper().buildQueryResult( object );
68              retVal = retVal.append( qr );
69              
70          }
71          
72          return retVal;
73      }
74  
75      /***
76       * @return Returns the instance.
77       */
78      public Object getInstance() {
79          return myInstance;
80      }
81      /***
82       * @param theInstance The instance to set.
83       */
84      public void setInstance( Object theInstance ) {
85          myInstance = theInstance;
86      }
87      /***
88       * @return Returns the method.
89       */
90      public Method getMethod() {
91          return myMethod;
92      }
93      /***
94       * @param theMethod The method to set.
95       */
96      public void setMethod( Method theMethod ) {
97          myMethod = theMethod;
98      }
99      /***
100      * @return Returns the methodDataSourceHelper.
101      */
102     public IMethodDataSourceHelper getMethodDataSourceHelper() {
103         return myMethodDataSourceHelper;
104     }
105     /***
106      * @param theMethodDataSourceHelper The methodDataSourceHelper to set.
107      */
108     public void setMethodDataSourceHelper( IMethodDataSourceHelper theMethodDataSourceHelper ) {
109         myMethodDataSourceHelper = theMethodDataSourceHelper;
110     }
111 }